/* Adiciona Autor */
package servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Autor;
import beans.Categoria;
import dbclass.AutorDAO;
import dbclass.AutorJDBC;
import dbclass.CategoriaDAO;
import dbclass.CategoriaJDBC;

/**
 * Servlet implementation class AutoresAdd
 */
public class AutoresAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutoresAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		//Verifica login
		if(session.getValue("autor_login") == null) {
			response.sendRedirect("Home?msg_erro=Acesso Negado");  return;
		} 
		
		String url = null;
		
		AutorDAO autorDAO = new AutorJDBC();
		
		String nome = request.getParameter("nome");
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		Autor autor = new Autor(0, login, senha, nome); 
		
		try { 
				 
			autorDAO.insert(autor); 
			url="Autores?msg_sucesso=Autor adicionado com sucesso";   
 
			response.sendRedirect(url); 
		} catch (ClassNotFoundException e) {  
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
